package com.fire.social.service.impl;

import com.fire.social.entity.node.Hobby;
import com.fire.social.entity.node.User;
import com.fire.social.mapper.FollowMapper;
import com.fire.social.mapper.HobbyMapper;
import com.fire.social.service.HobbyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class HobbyServiceImpl implements HobbyService {

    @Autowired
    private HobbyMapper hobbyMapper;
    @Autowired
    private FollowMapper followMapper;

    public boolean contains(List<Hobby> hobbies,Hobby target){
        for (Hobby hobby : hobbies) {
            if (hobby.getId().equals(target.getId()))
                return true;
        }
        return false;
    }

    @Override
    public void findHobbyByAccount(User user, Map<String, Object> map) {
        List<Hobby> hobbies = hobbyMapper.getAllHobbies();
        List<Hobby> myhobbies = hobbyMapper.getHobbyByAccount(user.getAccount());
        List<Hobby> res = new ArrayList<>();
        for (Hobby hobby : hobbies) {
            if (contains(myhobbies, hobby)) continue;
            res.add(hobby);
        }
        Long myFollowNum = followMapper.countIFollow(user.getAccount());
        Long followMeNum = followMapper.countFollowMe(user.getAccount());
        map.put("myfollowing", myFollowNum);
        map.put("follower", followMeNum);
        map.put("hobbies", res);
        map.put("myhobbies", myhobbies);
        map.put("user", user);
    }

    @Override
    public Hobby createHobby(String hname, String htype) {
        return hobbyMapper.createHobby(hname, htype);
    }

    @Override
    public List<Hobby> findHobbyByName(String hname) {
        return hobbyMapper.findHobbyByName(hname);
    }


}
